import 'package:flutter/material.dart';
import '../../model/station.dart';
import '../../model/stationcheck.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';
import '../../helper/Helper.dart';
import '../../helper/global_config.dart';
import '../../helper/uidata.dart';
import '../../model/report.dart';
import 'station_helper.dart';

class StationCheckAddPage extends StatefulWidget {
  @override
  StationCheckAddPage(this.station1, this.stc1, this.added);
  final Station station1;
  final StationCheck stc1;
  final VoidCallback added;
  @override
  StationCheckAddPageState createState() => new StationCheckAddPageState();
}

class StationCheckAddPageState extends State<StationCheckAddPage> {
  late StationCheck X1;

  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();
  final TextEditingController c5 = new TextEditingController();
  final TextEditingController c6 = new TextEditingController();
  final TextEditingController c7 = new TextEditingController();
  final TextEditingController c8 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.stc1 == null) {
      X1 = StationCheck(
          0,
          widget.station1.Guid,
          UIData.CurrentUser!.Guid,
          Helper.DateTimeNowString(),
          widget.station1.ID,
          widget.station1.STNM,
          "是",
          "是",
          "是",
          "是",
          "填写数值",
          "填写数值",
          "无",
          "无",
          "无",
          "",
          "",
          "",
          "",
          "");
    } else {
      X1 = widget.stc1;
    }

    c1.text = X1.DianYa;
    c2.text = X1.Rain1;
    c3.text = X1.Rain2;
    c4.text = X1.Works;
    c5.text = X1.CEquips;
    c6.text = X1.Problems;
    c7.text = X1.CMembers;
  }

  upload(BuildContext context) async {
    X1.DianYa = c1.text;
    X1.Rain1 = c2.text;
    X1.Rain2 = c3.text;
    X1.Works = c4.text;
    X1.CEquips = c5.text;
    X1.Problems = c6.text;
    X1.CMembers = c7.text;

    if (X1.ID == 0) {
      await StationCheck.Add(X1);
    } else {
      await StationCheck.Update(X1);
    }

    // widget.added();
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("添加检修记录"),
      ),
      body: StationCheckAddBody(context),
    );
  }

  void onWaiGuanChange(dynamic nn) {
    String yy = nn.toString();

    setState(() {
      X1.WaiGuan = yy;
    });

    //print(yy);
  }

  void onWeiXingChange(dynamic nn) {
    String yy = nn.toString();

    setState(() {
      X1.WeiXing = yy;
    });

    //print(yy);
  }

  void onDuanXinChange(dynamic nn) {
    String yy = nn.toString();
    // X1.DuanXin=yy;
    //print(yy);

    setState(() {
      X1.DuanXin = yy;
    });
  }

  void onRQMChange(String nn) {
    String yy = nn.toString();

    //print(yy);
    setState(() {
      X1.RecordQM = yy;
    });
  }

  void onEQMChange(String nn) {
    String yy = nn.toString();

    setState(() {
      X1.ExamQM = yy;
    });
  }

  StationCheckAddBody(BuildContext context) => SingleChildScrollView(
        child: StationCheckAddFields(context),
      );

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];

    list1.add(InputWidget.inCard(
      ListTile(
        leading: IconWidget.CheckImage(),
        title: Text(X1.StationName),
        trailing: Text(Helper.DateTimeFormat(X1.CreateTime)),
      ),
    ));

    list2.add(
      InputWidget.getSelect("外观检查是否正常", StationHelper.StateList2(), X1.WaiGuan,
          onChanged: onWaiGuanChange),
    );
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(
      InputWidget.getSelect("卫星发射是否正常", StationHelper.StateList2(), X1.WeiXing,
          onChanged: onWeiXingChange),
    );
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(
      InputWidget.getSelect("短信发射是否正常", StationHelper.StateList2(), X1.DuanXin,
          onChanged: onDuanXinChange),
    );
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("蓄电池及太阳能面板电压值", c1));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("雨量初始值", c2));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("雨量模拟后累计值", c3));
    list2.add(Divider(
      height: 0.0,
    ));
    list1.add(InputWidget.inCard2(list2));

    list3.add(InputWidget.getInput2("工作情况", c4));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInput2("更换设备", c5));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInput2("遗留问题", c6));
    list1.add(InputWidget.inCard2(list3));

    list4.add(InputWidget.getInput2("巡查人员", c7));
    list4.add(Divider(
      height: 0.0,
    ));
    //list4.add(InputWidget.getQMInput(context, "记录签字", X1.RecordQM, onRQMChange));

    // list4.add(InputWidget.getQMInput(context, "审核签字", X1.ExamQM, onEQMChange));
    list1.add(InputWidget.inCard2(list4));

    list1.add(
        InputWidget.inCard(InputWidget.getButton("保存", () => upload(context))));

    //list1.add(getCLWidget(s1.State==6, Helper.DateTimeFormat(s1.HZQMTM), s1.HZQM));
    return list1;
  }

  StationCheckAddFields(BuildContext context) {
    return Container(child: Column(children: getWidgetList(context)));
  }
}
